<template>
  <div>
    <!-- 收货地址列表 -->
    <div class="address-tabl-div">
      <el-table
        class="address-table"
        :data="tableData"
        stripe
        style="width: 100%; font-weight: 500"
        :header-cell-style="{ background: '#eee' }"
      >
        <el-table-column prop="aname" label="收货人"> </el-table-column>
        <el-table-column prop="city" label="所在地区"> </el-table-column>
        <el-table-column prop="address" label="详细地址" width="300">
        </el-table-column>
        <el-table-column prop="phone" label="手机号"></el-table-column>
        <el-table-column prop="operation" label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="默认地址">
          <template slot-scope="scope">
            <el-switch
              @change="changeValue(scope.row.id)"
              v-model="scope.row.default_address"
              active-color="#13ce66"
              inactive-color="#ccc"
            >
            </el-switch>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 点击编辑修改 -->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名">
          <el-input v-model="form.aname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-select
            v-model="form.efault_address"
            placeholder="是否设置为默认地址"
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateAdd">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 点击删除 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      form: {},
      dialogTableVisible: false,
      dialogFormVisible: false,
    };
  },
  methods: {
    // 截取城市方法
    getCity() {
      this.tableData.forEach((item) => {
        let city = item.address.split("市")[0];
        item.city = city;
      });
    },
    // 动态绑定默认地址方法
    changeValue() {
      // this.tableData.forEach((item) => {
      //   if (!item.default_address) {
      //     this.value = false;
      //   }
      // });
    },
    // 获取地址列表
    getAddList() {
      let user = sessionStorage.getItem("user");
      user = JSON.parse(user);
      let { uid } = user;
      this.$http.addressApi.queryByUid({ uid }).then((res) => {
        res.data.data.forEach((item) => {
          item.default_address = item.default_address == 1 ? true : false;
        });
        this.tableData = res.data.data;
        this.getCity();
        // console.log(this.tableData, "res2");
      });
    },
    // 点击修改按钮赋值
    handleEdit(a, form) {
      this.dialogFormVisible = true;
      this.form = form;
    },
    // 修改地址方法
    updateAdd() {
      console.log(1);
      this.$http.addressApi.update(this.form).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            type: "success",
            message: "修改成功",
          });
          this.dialogFormVisible = false;
        }
      });
    },
    // 删除地址方法
    handleDelete(a, b) {
      // console.log(a, b);
      this.$http.addressApi.delete({ id: b.id }).then((res) => {
        this.tableData.splice(a, 1);
      });
    },
  },
  mounted() {
    this.getAddList();
  },
};
</script>

<style lang="scss" scoped>
.address-tabl-div {
  width: 90%;
  margin: 40px auto 0;
}
</style>